<template>
  <el-dialog
    :visible="isShow"
    width="630px"
    :close-on-click-modal="false"
    :title="title"
    @close="handlerClose"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="区域名称" label-width="140px" prop="regionName">
        <el-input v-model="form.regionName" show-word-limit maxlength="15" />
      </el-form-item>
      <el-form-item
        label="备注说明"
        label-width="140px"
        prop="remark"
        class="remark_form_item"
      >
        <el-input
          v-model="form.remark"
          type="textarea"
          show-word-limit
          maxlength="40"
          :autosize="{ minRows: 4 }"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="footer">
        <el-button class="cancle" type="primary" @click="handlerClose">
          取消
        </el-button>
        <el-button class="confirm" type="primary" @click="submit">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { addRegionApi, updateRegionApi } from '@/api/pointManage/region'
export default {
  name: 'QuyuFormDialog',
  data () {
    return {
      isShow: false,
      title: '',
      form: {
        regionName: '',
        remark: ''
      },
      rules: {
        regionName: [{ required: true, trigger: 'blur', message: '请输入' }],
        remark: [{ required: true, trigger: 'blur', message: '请输入' }]
      }
    }
  },
  methods: {
    handlerOpen (type, row) {
      if (type === 'add') {
        this.title = '新增区域'
      } else {
        this.title = '修改区域'
        const obj = {}
        for (const k in row) {
          if (k === 'name') {
            obj.regionName = row[k]
          } else {
            obj[k] = row[k]
          }
        }
        this.form = obj
      }
      this.isShow = true
    },
    handlerClose () {
      this.form = {
        regionName: '',
        remark: ''
      }
      this.$refs.form.resetFields()
      this.isShow = false
    },
    async submit () {
      try {
        await this.$refs.form.validate()
        if (!this.form.id) {
          await addRegionApi(this.form)
          this.$message.success('添加成功')
        } else {
          await updateRegionApi(this.form)
          this.$message.success('修改成功')
        }
        this.handlerClose()
        this.$emit('getList')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input {
  width: 396px;
  height: 36px;
  ::v-deep .el-input__inner {
    height: 36px;
  }
}
::v-deep .el-dialog {
  border-radius: 10px;
}
::v-deep .el-dialog__body {
  padding: 20px 20px 30px;
}
::v-deep .el-dialog__footer {
  padding: 0;
  padding-bottom: 40px;
}
.last_form_item {
  margin: 0;
}
.footer {
  text-align: center;
}
.cancle {
  background-color: #fbf4f0 !important;
  color: #655b56 !important;
  border: none;
}
.confirm {
  margin-left: 34px !important;
  background: linear-gradient(135deg, #ff9743, #ff5e20) !important;
  border: none;
}
.el-textarea {
  width: 396px;
}
.remark_form_item {
  margin: 0;
}
</style>
